//
//  AppDelegate.m
//  HonlivApp
//
//  Created by admin on 2017/6/23.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginController.h"
#import "MainTabbarController.h"
#import "CarsTBViewController.h"
#import "RemindNewsViewController.h"

#import "NetWorker.h"


#define deviceVersion [[[UIDevice currentDevice] systemVersion] floatValue]

@interface AppDelegate ()

@property (nonatomic,strong) UIApplicationShortcutItem *cutItem;

@end

@implementation AppDelegate


//登录过期判断
-(void)applicationDidBecomeActive:(UIApplication *)application{
    //没有开指纹解锁 并且已经登录
    if (![NetWorker sharedNetWorker].isTouchId && [NetWorker sharedNetWorker].isLogin) {
        //判断是否过期
       BOOL expried = [[NetWorker sharedNetWorker] updateExpriedStatue];
            if (expried) {
                [[NSNotificationCenter defaultCenter] postNotificationName:expriLogin object:nil];
            }
    }else if (![NetWorker sharedNetWorker].isLogin){
        self.window.rootViewController = [[LoginController alloc] init];
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //同意HUD设置
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setMinimumDismissTimeInterval:1];
    
    //导航栏设置
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.barStyle = UIBarStyleBlackTranslucent;
    //背景颜色
    [bar setBarTintColor:BAR_BGCOLOR];
    //文字颜色（标题的颜色）
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //系统自带导航的文字颜色 比如系统默认返回按钮的颜色
    [bar setTintColor:[UIColor whiteColor]];
    //返回按钮  不显示返回文字
//    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.window makeKeyAndVisible];

    if (deviceVersion >= 9.0) {
        [self setup3DTouch:application];
    }
    
    //是否登录
    if ([NetWorker sharedNetWorker].isLogin) {
        MainTabbarController *mainV = [[MainTabbarController alloc] init];
        self.window.rootViewController = mainV;
    }
    else{
        self.window.rootViewController = [[LoginController alloc] init];
    }
    
    return YES;
}

//代码设置
-(void)setup3DTouch:(UIApplication *)application{
    /*
     type：改item唯一标识符
     localizedTitle:标题
     localizedSubTitle:副标题
     icon:icon图标，可以使用系统类型，也可以使用自定义的图片
     userInfo:用户信息字典 自定义参数，完成具体功能需求
     */
    UIApplicationShortcutIcon *cameraIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeTask];
    UIApplicationShortcutItem *cameraItem = [[UIApplicationShortcutItem alloc] initWithType:@"ONE" localizedTitle:@"出差车辆" localizedSubtitle:@"" icon:cameraIcon userInfo:nil];
    
    UIApplicationShortcutIcon *shareIcon = [UIApplicationShortcutIcon iconWithType:UIApplicationShortcutIconTypeAlarm];
    UIApplicationShortcutItem *shareItem = [[UIApplicationShortcutItem alloc] initWithType:@"TWO" localizedTitle:@"温馨提醒" localizedSubtitle:@"" icon:shareIcon userInfo:nil];
    
    //将Item添加到APP图标
    application.shortcutItems = @[cameraItem,shareItem];
    
}

//3D touch 重压响应事件
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler{

    if ([shortcutItem.type isEqualToString:@"ONE"]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:cars object:nil];

    }else if ([shortcutItem.type isEqualToString:@"TWO"]){
        [[NSNotificationCenter defaultCenter] postNotificationName:remind object:nil];

    }

}
//程序从后台切换到前台
-(void)applicationWillEnterForeground:(UIApplication *)application{
    [[NSNotificationCenter defaultCenter] postNotificationName:touchId object:nil];
    //    [self judgeTouchId];
}

-(BOOL)shouldAutorotate{
    return NO;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

//程序即将挂起
-(void)applicationWillResignActive:(UIApplication *)application{
    [[NetWorker sharedNetWorker] setLoginTime];
}


@end
